<template>
  <a-modal
    title="集中抄錶"
    :width="600"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @cancel="handleCancel"
  >
    <template slot="footer">
        <a-button key="back" @click="handleCancel">
          取消
        </a-button>
        <a-button key="submit" type="primary"  @click="handleSubmit">
          開始换
        </a-button>
    </template>
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item v-show="false"><a-input v-decorator="['id']" /></a-form-item>
        <a-form-item
          label="房產"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol">
                <a-select v-decorator="['estateId', {  rules: [{ required: true, message: '請選擇房產！' }]}]" style="width: 100%">
                  <a-select-option  style="width: 100%"  v-for="(item,index) in stateList" :key="index" :value="item.id">{{ item.name }}</a-select-option>
                </a-select>
        </a-form-item>
        <a-form-item
          label="表類型"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol">
                 <a-select v-decorator="['type', {  rules: [{ required: true, message: '請選擇表類型！' }]}]" style="width: 100%">
                  <a-select-option  style="width: 100%"  v-for="(item,index) in typeList" :key="index" :value="item.id">{{ item.name }}</a-select-option>
                </a-select>
        </a-form-item>
      </a-form>
    </a-spin>
    <edit-huan-biao-form ref="editHuanBiaoForm"/>
  </a-modal>
</template>

<script>
  import moment from 'moment'
  import { getAllStateList } from '@/api/modular/main/busrealestate/busRealEstateManage'
  import { busCostBillHydropowerList } from '@/api/modular/main/buscostbillhydropower/busCostBillHydropowerManage'
  import editHuanBiaoForm from './editHuanBiaoForm.vue'
  export default {
    components: {
      editHuanBiaoForm
    },
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        meterReadingDateString: '',
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this),
        stateList: [],
        typeList: []
      }
    },
    methods: {
      moment,
      // 初始化方法
      edit () {
        this.visible = true
        this.getStateList()
        this.getCostBillHydropowerList()
      },
      getStateList() {
        getAllStateList().then((res) => {
          if (res.code === 200) {
            this.stateList = res.data
          } else {
            this.$message.error(res.msg) // + res.message
          }
        })
      },
      getCostBillHydropowerList() {
        busCostBillHydropowerList().then((res) => {
          if (res.code === 200) {
            this.typeList = res.data
          } else {
            this.$message.error(res.msg) // + res.message
          }
        })
      },
      handleSubmit () {
        const { form: { validateFields } } = this
        validateFields((errors, values) => {
          if (!errors) {
            for (const key in values) {
              if (typeof (values[key]) === 'object') {
                values[key] = JSON.stringify(values[key])
              }
            }
            this.$refs.editHuanBiaoForm.edit(values)
          }
        })
      },
      onChangerentedDay(date, dateString) {
        this.meterReadingDateString = dateString
      },
      handleCancel () {
        this.$emit('ok', '')
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
